package thread.Semaphore;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class SemaphoreTest implements Runnable {

    public static void main(String[] args) {

        for (int i = 0;i<30;i++){
            SemaphoreTest semaphoreTest = new SemaphoreTest();
            Thread thread  = new Thread(semaphoreTest);
            thread.start();
        }
    }
    static Semaphore semaphore = new Semaphore(5,true);

    @Override
    public void run() {

        try {
            semaphore.acquire();
            System.out.println(Thread.currentThread().getName()+"执行业务代码");
            TimeUnit.SECONDS.sleep(5);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            semaphore.release();
        }
    }
}
